package com.haust.su.ghyy.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haust.su.ghyy.common.exception.YyghException;
import com.haust.su.ghyy.common.result.Result;
import com.haust.su.ghyy.common.utils.MD5;
import com.haust.su.ghyy.hospital.service.HospitalSetService;
import com.haust.su.ghyy.hospital.service.Impl.HospitalSetServiceImpl;
import com.haust.su.ghyy.model.hosp.HospitalSet;
import com.haust.su.ghyy.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;
import java.util.Random;

/**
 * @author haustsusu
 * @create 2023-02-13-13:57
 */
//默认返回的是json数据,使用Jackson进行json转换
@Api(tags="医院增删改查部分")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    //@GetMapping
    //public List<HospitalSet> getHospitalSet(){
    //    List<HospitalSet> list = hospitalSetService.list();
    //    return list;
    //}


    //@Autowired
    //private HospitalSetServiceImpl hospitalSetService;


    @ApiOperation(value = "获取所有医院信息")
    @GetMapping
    public Result getHospitalSet(){
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }

    //逻辑删除对应医院
    //@ApiModelProperty(value = "逻辑删除(1:已删除，0:未删除)")
    //@TableLogic
    //@TableField("is_deleted")
    //private Integer isDeleted;
    //@APIOperation注释可以太swagger中出现注释
    @ApiOperation(value = "逻辑删除医院操作")
    @DeleteMapping("{id}")
    public Result DeleteHospitalSet(@PathVariable Long id){

        boolean bool = hospitalSetService.removeById(id);

        if (bool){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



    //分页查询接口
    //
    @ApiOperation(value = "条件查询带分页功能操作")
    @PostMapping("GetByPages/{current}/{limit}")
    public Result GetByPages(@PathVariable long current,
                              @PathVariable long limit,
                             @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建mp中的page对象,传递当前页以及
        Page<HospitalSet> page = new Page<>(current, limit);
        //为分页查询new一个条件对象
        //wrapperd对象为空无法进行查询
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        //获取条件查询对象中的两个值
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //做判断
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //返回分页查询结果setpage
        Page<HospitalSet> setPage = hospitalSetService.page(page, wrapper);
        //将结果统一放入result对象内
        Result<Page<HospitalSet>> result = Result.ok(setPage);
        return result;
    }




    //添加医院设置
    @ApiOperation("添加医院设置")
    @PostMapping("SaveHospitalSet")
    public Result SaveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(0);
        Random random = new Random();
        System.out.println(random.nextInt(100));
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);

        if (save){
           return Result.ok();
        }else{
           return Result.fail();
        }

    }


    //根据id获取医院信息设置
    @ApiOperation("根据id获取医院信息设置")
    @GetMapping("GetHospitalSetById/{id}")
    public Result GetHospitalSetById(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        System.out.println(hospitalSet);//查询不到的数据返回null
        Result<HospitalSet> ok = Result.ok(hospitalSet);
        return ok;

    }



    //修改医院信息设置
    @ApiOperation("修改医院的设置接口")
    @PostMapping("UpdateHospital")
    public Result UpdateHospital(@RequestBody HospitalSet hospitalSet){
        boolean bool = hospitalSetService.updateById(hospitalSet);
        if (bool){
           return Result.ok();
        }else {
           return Result.fail();
        }
    }



    //批量删除医院信息设置
    @ApiOperation("批量删除医院信息设置")
    @DeleteMapping("deleteHospitalByBatch")
    public Result BatchRemoveHospitalset(@RequestBody List<Long> idlist){
        boolean bool = hospitalSetService.removeByIds(idlist);
        if (bool){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }



    //医院设置锁定与解锁功能
    @ApiOperation("医院设置锁定与解锁功能")
    @PutMapping("IfLockHospitalSet/{id}/{status}")
    public Result IfLockHospitalSet(@PathVariable Long id,
                                    @PathVariable int status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean bool = hospitalSetService.updateById(hospitalSet);
        System.out.println(bool);
            return Result.ok();
    }



    //发送签名秘钥
    @ApiOperation("发送签名秘钥操作")
    @PutMapping("sendKey/{id}")
    public Result SendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();



        return Result.ok();
    }

}
